package com.example.text.ui.login.viewmodel
import androidx.lifecycle.MutableLiveData
import com.example.text.base.BaseViewModel
import com.example.text.base.state.Default
import com.example.text.base.state.Empty
import com.example.text.base.state.Failed
import com.example.text.base.state.Loading
import com.example.text.base.state.RequestEvent
import com.example.text.base.state.Success
import com.example.text.ui.mall.model.GoodEntity
import com.example.text.ui.login.repository.LoginRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import javax.inject.Inject

@HiltViewModel
class LoginViewModel @Inject constructor(private val repository: LoginRepository): BaseViewModel() {

    val phone: MutableStateFlow<String> = MutableStateFlow("")
    val password: MutableStateFlow<String> = MutableStateFlow("")

    //保存当前正在显示的fragment
    private val _currentFragment = MutableStateFlow("")
    val currentFragment: StateFlow<String>
        get() = _currentFragment

    fun setShowingFragmentTag(tag: String) {
        _currentFragment.value = tag
    }

    fun clearPhone() {
        phone.value = ""
    }

    fun clearPassword() {
        password.value = ""
    }





    protected val _stateListener=MutableStateFlow<RequestEvent>(Default)
    val stateListener get() = _stateListener.asStateFlow()
     val getGoodsInfoLiveData by lazy {
         MutableLiveData<List<GoodEntity>>()
     }
    //val getGoodsInfoLiveData: LiveData<List<GoodEntity>> = _getGoodsInfoLiveData

//    fun getGoodsInfo(){
//        execute(
//            action = repository.getGoodsInfo(),
//            onStart = {
//                //开始请求
//                _stateListener.value = Loading
//            },
//            onError = {
//                //请求失败
//                _stateListener.value = Failed(it)
//            },
//            onFinish = {
//                //请求结束
//                if (it.data == null){
//                    _stateListener.value = Empty
//                }else{
//                    _stateListener.value = Success
//                    getGoodsInfoLiveData.value=it.data
//                }
//
//            }
//        )
//    }



}